/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: DEVICE_ID_SRF08.java Package: tc.de.krawallmann.robobridge.devices.implementation
 * Version: 1.0 Author(s): KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.devices.implementation;

import tc.de.krawallmann.robobridge.communication.Command;
import tc.de.krawallmann.robobridge.communication.CommandType;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.DistanceSensor;
import tc.de.krawallmann.robobridge.devices.LightSensor;
import tc.de.krawallmann.robobridge.devices.Robot;
import tc.de.krawallmann.robobridge.util.BitOperations;

/**
 * The Class DEVICE_ID_SRF08 represents an ultrasonic range sensor combined with a light
 * sensor.<br/>
 * It is made by Devantech.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class SRF08 extends AbstractDevice implements DistanceSensor, LightSensor {

	public static final String SRF08_DISTANCE = "SRF08_DISTANCE";

	public static final String SRF08_LIGHT = "SRF08_LIGHT";
	/** The distance. */
	private transient int distance;

	/** The light value. */
	private transient int lightValue;

	/**
	 * Instantiates a new DEVICE_ID_SRF08.
	 * 
	 * @param robot
	 *            the robot
	 * @param deviceId
	 *            the device id
	 */
	public SRF08(final Robot robot, final int deviceId) {
		super(robot, AbstractDevice.DEVICE_ID_SRF08, deviceId, "Devantech", "DEVICE_ID_SRF08");
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.DistanceSensor#getDistance()
	 */
	@Override
	public int getDistance() {
		return distance;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * tc.de.krawallmann.robobridge.devices.implementation.LightSensor#getLight
	 * ()
	 */
	@Override
	public int getLightValue() {
		return lightValue;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * basepackage.IncomingCommandListener#onCommandReceived(basepackage.Command
	 * )
	 */
	@Override
	public boolean onCommandReceived(final Command command) {
		if (containsRelevantData(command, Integer.class, Integer.class, Integer.class)) {
			final int oldDistance = distance;
			final int oldLight = lightValue;
			distance =
					BitOperations.combineHighLow((Integer) command.getParameter(1),
							(Integer) command.getParameter(2));
			lightValue = (Integer) command.getParameter(3);
			firePropertyChange(SRF08_DISTANCE, oldDistance, distance);
			firePropertyChange(SRF08_LIGHT, oldLight, lightValue);
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.AbstractDevice#toString()
	 */
	@Override
	public String toString() {
		return "DEVICE_ID_SRF08 [deviceId=" + deviceId + ", manufacturer=" + manufacturer + ", model="
				+ model + ", distance=" + distance + ", lightValue=" + lightValue + "]";
	}

	public void measure() {
		robot.queueOutgoingCommand(new Command(CommandType.UPDATE_ROBOT_DEVICE, false, getDeviceId(), 1));
	}
}
